package com.song.tradition.page.home

import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.song.tradition.R

import com.song.tradition.entity.grassData
import com.song.tradition.entity.storyData
import com.song.tradition.page.components.CategoryItem
import com.song.tradition.page.components.LinearStoryItemCard
import com.song.tradition.page.components.ScreenRoute
import com.song.tradition.page.components.TitleWithMoreTab
import com.song.tradition.ui.theme.*

@SuppressLint("ResourceType")
@Composable
fun HomeScreen(
    navController: NavController,
) {


    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(customBgColor)
            .padding(start = 16.dp, end = 16.dp, bottom = 8.dp)
            .verticalScroll(
                rememberScrollState()
            )
    ) {

        Spacer(modifier = Modifier.height(16.dp))

        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically
        ) {

            Card(
                modifier = Modifier.size(56.dp),
                elevation = CardDefaults.cardElevation(2.dp),
                shape = CircleShape,
                border = BorderStroke(1.dp, color = text200.copy(alpha = 0.5f))
            ) {
                Image(painterResource(id = R.drawable.avatar), contentDescription = "")
            }
            Spacer(modifier = Modifier.width(16.dp))

            Text(text = "Song", fontSize = 24.sp, fontWeight = FontWeight.Bold)
        }

        Spacer(modifier = Modifier.height(16.dp))

        Text(
            text = "欢迎来到中医药文化世界！",
            fontSize = 20.sp,
            fontWeight = FontWeight.Bold,
            color = text100
        )

        Spacer(modifier = Modifier.height(16.dp))

        Row(modifier = Modifier.fillMaxWidth()) {

            Card(
                shape = RoundedCornerShape(10.dp), modifier = Modifier
                    .height(48.dp)
                    .weight(1f),
                elevation = CardDefaults.cardElevation(3.dp)
            ) {
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier
                        .height(48.dp)
                        .fillMaxWidth()
                        .background(customCardBgColor)
                        .clickable {
                            navController.navigate(ScreenRoute.SearchScreen.route)
                        }


                ) {
                    Spacer(modifier = Modifier.width(16.dp))

                    Icon(
                        imageVector = Icons.Default.Search, contentDescription = "",
                        tint = Color.Gray
                    )
                    Spacer(modifier = Modifier.width(8.dp))

                    Text(text = "搜索", color = Color.Gray)
                }

            }



            Spacer(modifier = Modifier.width(16.dp))
            Card(
                Modifier.size(48.dp),
                shape = RoundedCornerShape(10.dp),
                elevation =CardDefaults.cardElevation(2.dp)
            ) {
                IconButton(
                    onClick = { /*TODO*/ },
                    modifier = Modifier
                        .fillMaxSize()
                        .background(customBgColor)

                ) {
                    Icon(
                        painterResource(id = R.drawable.setting_icon), contentDescription = "",
                        modifier = Modifier.size(24.dp)
                    )

                }
            }

        }

        Spacer(modifier = Modifier.height(8.dp))

//        标题栏
        TitleWithMoreTab(title = "识百草", click = {

        })

//
        LazyHorizontalGrid(modifier = Modifier
            .fillMaxWidth()
            .height(200.dp),
            rows = GridCells.Fixed(1),
            horizontalArrangement = Arrangement.spacedBy(12.dp),
            content = {

                items(grassData.size) { index: Int ->
                    CategoryItem(data = grassData[index], modifier = Modifier.clickable {

                        navController.navigate(ScreenRoute.GrassDetailScreen.route + "/${index}")
                    })
                }
            }
        )


//        标题栏
        TitleWithMoreTab("中医小故事", click = {

        })


        Column(modifier = Modifier.fillMaxWidth()) {
            repeat(storyData.size) { index ->
                LinearStoryItemCard(data = storyData[index],
                    modifier = Modifier.clickable {
                        Log.d("TAG", "HomeScreen: Click")
                        navController.navigate(ScreenRoute.StoryDetailScreen.route + "/${index}")

                    })
            }
        }


    }

}


@Preview(showBackground = true)
@Composable
fun HomeScreenTest() {
    HomeScreen(navController = rememberNavController())
}